<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>消息队列工作原理 - RocketMQ</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9f9f9;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
            color: #222;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .highlight-box {
            border-left: 4px solid #6e8efb;
            background-color: rgba(110, 142, 251, 0.05);
        }
        .step-number {
            display: inline-block;
            width: 32px;
            height: 32px;
            background-color: #6e8efb;
            color: white;
            border-radius: 50%;
            text-align: center;
            line-height: 32px;
            margin-right: 10px;
            font-weight: bold;
        }
        .mermaid {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        }
        .interaction-point {
            position: relative;
            padding-left: 30px;
        }
        .interaction-point:before {
            content: "";
            position: absolute;
            left: 0;
            top: 10px;
            width: 16px;
            height: 16px;
            background-color: #a777e3;
            border-radius: 50%;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <div class="mb-6">
                    <span class="inline-block px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-semibold tracking-wider">分布式消息队列</span>
                </div>
                <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">RocketMQ 核心工作原理</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-10 max-w-3xl leading-relaxed">深入解析 Producer 和 Consumer 的完整工作流程，了解消息系统的核心机制</p>
                <div class="flex space-x-4">
                    <a href="#producer" class="px-6 py-3 bg-white text-indigo-600 font-semibold rounded-full hover:bg-gray-100 transition duration-300">
                        <i class="fas fa-paper-plane mr-2"></i>Producer流程
                    </a>
                    <a href="#consumer" class="px-6 py-3 bg-white bg-opacity-20 text-white font-semibold rounded-full hover:bg-opacity-30 transition duration-300">
                        <i class="fas fa-inbox mr-2"></i>Consumer流程
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-5xl">
        <!-- Producer Section -->
        <section id="producer" class="mb-24">
            <div class="flex items-center mb-10">
                <div class="w-12 h-12 rounded-full bg-indigo-100 flex items-center justify-center mr-6">
                    <i class="fas fa-paper-plane text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">Producer 发送消息的完整流程</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <span class="step-number">1</span>
                        启动初始化阶段
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>创建并启动 Producer 实例</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>加载配置并初始化内部组件</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>与 NameServer 建立连接，获取 Topic 路由信息</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <span class="step-number">2</span>
                        消息发送前准备
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>创建消息对象 (Message)，设置 Topic、Tag、Key 等属性</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>根据需要设置消息属性，如延迟级别、事务标识等</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-indigo-400 mt-1 mr-3"></i>
                            <span>序列化消息体</span>
                        </li>
                    </ul>
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6 mb-12">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-indigo-600 flex items-center">
                        <span class="step-number">3</span>
                        路由查询
                    </h3>
                    <p>根据 Topic 从本地路由表查询消息队列 (MessageQueue) 列表</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-indigo-600 flex items-center">
                        <span class="step-number">4</span>
                        消息发送
                    </h3>
                    <p>根据发送模式执行不同的发送逻辑（同步/异步/单向）</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-indigo-600 flex items-center">
                        <span class="step-number">5</span>
                        重试机制
                    </h3>
                    <p>当消息发送失败时进行有限次数重试</p>
                </div>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md mb-12 card-hover">
                <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                    <span class="step-number">6</span>
                    结果处理
                </h3>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="highlight-box p-4 rounded">
                        <h4 class="font-semibold mb-2"><i class="fas fa-file-alt mr-2 text-indigo-400"></i>记录日志</h4>
                        <p class="text-sm">记录发送结果日志以供后续分析</p>
                    </div>
                    <div class="highlight-box p-4 rounded">
                        <h4 class="font-semibold mb-2"><i class="fas fa-chart-bar mr-2 text-indigo-400"></i>统计信息</h4>
                        <p class="text-sm">处理发送统计信息监测系统健康度</p>
                    </div>
                    <div class="highlight-box p-4 rounded">
                        <h4 class="font-semibold mb-2"><i class="fas fa-bell mr-2 text-indigo-400"></i>回调处理</h4>
                        <p class="text-sm">触发相应的成功或失败回调通知业务方</p>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Consumer Section -->
        <section id="consumer" class="mb-24">
            <div class="flex items-center mb-10">
                <div class="w-12 h-12 rounded-full bg-purple-100 flex items-center justify-center mr-6">
                    <i class="fas fa-inbox text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">Consumer 接收和处理消息的步骤</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-purple-600 flex items-center">
                        <span class="step-number">1</span>
                        启动与订阅
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-400 mt-1 mr-3"></i>
                            <span>创建 Consumer 实例并注册消息监听器</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-400 mt-1 mr-3"></i>
                            <span>订阅 Topic 和子主题 (Tag)</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-purple-400 mt-1 mr-3"></i>
                            <span>从 NameServer 获取路由信息</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-purple-600 flex items-center">
                        <span class="step-number">2</span>
                        消息拉取
                    </h3>
                    <div class="mb-4">
                        <h4 class="font-medium text-purple-500 mb-2"><i class="fas fa-long-arrow-alt-down mr-2"></i>Push 模式</h4>
                        <p class="text-sm pl-6">长轮询机制，Broker 没有新消息时会挂起请求</p>
                    </div>
                    <div>
                        <h4 class="font-medium text-purple-500 mb-2"><i class="fas fa-hand-paper mr-2"></i>Pull 模式</h4>
                        <p class="text-sm pl-6">应用主动调用拉取 API 获取消息</p>
                    </div>
                </div>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6 mb-12">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-purple-600 flex items-center">
                        <span class="step-number">3</span>
                        消息处理
                    </h3>
                    <ul class="text-sm space-y-2">
                        <li>提交到消费线程池</li>
                        <li>执行反序列化</li>
                        <li>Tag 过滤</li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-purple-600 flex items-center">
                        <span class="step-number">4</span>
                        ACK 确认
                    </h3>
                    <ul class="text-sm space-y-2">
                        <li>成功时提交消费位点</li>
                        <li>失败时进入重试队列</li>
                        <li>支持立即重试或延迟重试</li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-purple-600 flex items-center">
                        <span class="step-number">5</span>
                        位点管理
                    </h3>
                    <ul class="text-sm space-y-2">
                        <li>定期持久化消费位点</li>
                        <li>集群模式保存在 Broker</li>
                        <li>广播模式保存在本地</li>
                    </ul>
                </div>
            </div>
        </section>
        
        <!-- Interaction Diagram -->
        <section class="mb-24">
            <div class="flex items-center mb-10">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center mr-6">
                    <i class="fas fa-exchange-alt text-blue-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold">Producer 和 Consumer 之间的交互过程</h2>
            </div>
            
            <div class="bg-white p-6 rounded-xl shadow-md mb-12">
                <div class="mermaid">
                    sequenceDiagram
                        participant P as Producer
                        participant B as Broker
                        participant C as Consumer
                        
                        P->>B: 1. 启动并注册
                        C->>B: 1. 启动并注册
                        P->>B: 2. 获取路由信息
                        C->>B: 2. 获取路由信息
                        P->>B: 3. 发送消息
                        B->>P: 4. 确认接收
                        C->>B: 5. 订阅Topic
                        C->>B: 6. 拉取消息请求
                        B->>C: 7. 返回消息数据
                        C->>B: 8. 提交消费位点(ACK)
                </div>
            </div>
            
            <div class="bg-white p-8 rounded-xl shadow-md card-hover">
                <h3 class="text-2xl font-semibold mb-6 text-blue-600">关键交互点详解</h3>
                
                <div class="space-y-6">
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">消息路由获取</h4>
                        <p>Producer 和 Consumer 都需要从 NameServer 获取 Topic 路由信息，路由信息包含 Topic 所在的 Broker 地址、读写队列数等。</p>
                    </div>
                    
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">消息队列分配</h4>
                        <p>每个 Topic 包含多个消息队列，分布在不同的 Broker 上。Consumer 通过负载均衡策略分配队列，确保处理负载均衡。队列分配会在 Consumer 数量变化时触发重新平衡。</p>
                    </div>
                    
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">消息持久化</h4>
                        <p>Producer 发送消息到 Broker 后，Broker 先将消息持久化到磁盘。持久化完成后，Broker 返回确认响应给 Producer。</p>
                    </div>
                    
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">消息消费模式</h4>
                        <p>Push 模式：Broker 主动推送消息到 Consumer（内部实现为长轮询）。Pull 模式：Consumer 主动从 Broker 拉取消息。</p>
                    </div>
                    
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">消费确认机制</h4>
                        <p>Consumer 处理完消息后，需向 Broker 提交消费位点。支持集群消费（消息只被一个 Consumer 实例消费）和广播消费（消息被所有 Consumer 实例消费）。</p>
                    </div>
                    
                    <div class="interaction-point">
                        <h4 class="text-lg font-semibold mb-2">重试与死信机制</h4>
                        <p>消费失败的消息会进入重试队列。多次重试失败后，消息进入死信队列等待人工处理。</p>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Summary -->
        <section class="bg-indigo-50 p-8 rounded-xl">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-indigo-600 text-2xl mr-4"></i>
                <h3 class="text-2xl font-bold">系统设计要点</h3>
            </div>
            <p class="mb-6">Producer 和 Consumer 通过 Broker 实现解耦，使得生产者和消费者可以独立扩展，整个工作流程确保了消息的可靠传递和高效处理。</p>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <h4 class="font-semibold mb-2 text-indigo-600"><i class="fas fa-link mr-2"></i>解耦设计</h4>
                    <p class="text-sm">生产者和消费者完全解耦，互不影响</p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <h4 class="font-semibold mb-2 text-indigo-600"><i class="fas fa-bolt mr-2"></i>高性能</h4>
                    <p class="text-sm">基于长轮询的高效消息推送机制</p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <h4 class="font-semibold mb-2 text-indigo-600"><i class="fas fa-shield-alt mr-2"></i>可靠性</h4>
                    <p class="text-sm">完善的重试和确认机制保证消息不丢失</p>
                </div>
            </div>
        </section>
    </main>
    
    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto px-6 max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <h3 class="text-xl font-semibold mb-2 text-white">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition duration-300">
                    http://www.yuque.com/jtostring
                </a>
                <div class="mt-6 flex space-x-4">
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-indigo-600 transition duration-300">
                        <i class="fab fa-github"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-indigo-600 transition duration-300">
                        <i class="fab fa-twitter"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-indigo-600 transition duration-300">
                        <i class="fab fa-linkedin-in"></i>
                    </a>
                </div>
            </div>
        </div>
    </footer>
    
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>